package com.lie.prepare.algorithm.practise;

import static com.lie.prepare.util.Print.*;

/**
 * Created by lie on 2018/4/12.
 * 二分法的抽象
 */
public abstract class ABS {

    //length由array获得
    protected int length;

    protected int doSearch(){
        int start = 0;
        int end = length-1;
        int half;

        while (end - start >= 0) {
            half = (start + end) / 2;
            if (equalOrNot(half)) {
                return half;
            } else if (largerThanKey(half)) {
                end = half -1;
            }else {
                start = half + 1;
            }
        }
        return -1;
    }

    abstract boolean largerThanKey(int index);
    abstract boolean equalOrNot(int index);

}
